﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Miris.HumanMilkAnalyser.DataModel;
using Miris.HumanMilkAnalyser.Repository;
using Miris.HumanMilkAnalyser.Repository.Interface;

namespace Miris.HumanMilkAnalyser.Repository.EntityFramework
{
    public class DiagnosticMessageReferenceRepository : RepositoryBase<DiagnosticMessageReference>, IDiagnosticMessageReferenceRepository
    {
        public DiagnosticMessageReferenceRepository(MirisHumanMilkAnalyserContext dataContext) : base(dataContext) { }

        /// <summary>
        /// Add or update DiagnosticMessageReference.
        /// </summary>
        /// <param name="entity">The DiagnosticMessageReference will be updated if it is existing or added if it cannot be found.</param>
        public override void AddOrUpdate(DiagnosticMessageReference entity)
        {
            var matchedMessageCount = GetRowCount(m => m.MessageTimestamp == entity.MessageTimestamp);

            if (matchedMessageCount == 0)
            {
                Add(entity);
            }
            else
            {
                Update(entity);
            }
        }
    }
}
